<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="清单名称" prop="sheetName">
        <el-input v-model="queryParams.sheetName" placeholder="请输入清单名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row :gutter="10" class="mb8 mb8 cd-pr8 top-right-btn small-line-height">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd(1)"
            v-hasPermi="['base:standardSheet:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['base:standardSheet:export']">导出</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="standardSheetList" row-key="id" border default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column label="清单名称" header-align="center" prop="label" min-width="180" />
      <el-table-column label="清单id" align="center" prop="id" width="80" />
      <el-table-column label="清单编号" header-align="center" prop="sheetCode" min-width="120" />
      <el-table-column label="清单单位" align="center" prop="sheetUnit" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.sheetUnit ? scope.row.sheetUnit : "-" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="税率（%）" align="center" prop="sheetTaxRate" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.sheetTaxRate ? scope.row.sheetTaxRate + "%" : "-" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.remark ? scope.row.remark : "-" }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="排序" align="center" prop="weight" width="80">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-tag v-if="scope.row.sheetLevel == '1'">{{ scope.row.weight }}</el-tag>-->
      <!--          <el-tag v-if="scope.row.sheetLevel == '2'" type="success">{{ scope.row.weight }}</el-tag>-->
      <!--          <el-tag v-if="scope.row.sheetLevel == '3'" type="info">{{ scope.row.weight }}</el-tag>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button v-if="scope.row.sheetLevel != 3" size="mini" type="text" icon="el-icon-plus"
            @click="handleAdd(scope.row)" v-hasPermi="['base:standardSheet:edit']">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['base:standardSheet:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['base:standardSheet:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改标准投标清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="清单名称" prop="sheetName">
          <el-input v-model="form.sheetName" @input="changeMessage" placeholder="请输入清单名称" />
        </el-form-item>
        <el-form-item label="清单单位" prop="sheetUnit">
          <el-input v-model="form.sheetUnit" placeholder="请输入清单单位" />
        </el-form-item>
        <el-form-item label="税率（%）" prop="sheetTaxRate">
          <el-input v-model="form.sheetTaxRate" placeholder="请输入税率（%）" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!--        <el-form-item label="排序" prop="sort">-->
        <!--          <el-input v-model="form.sort" placeholder="请输入排序" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStandardSheet, delStandardSheet, addStandardSheet, updateStandardSheet } from "@/api/base/standardSheet";

export default {
  name: "StandardSheet",
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      // 标准投标清单表格数据
      standardSheetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: undefined,
        sort: undefined,
        sheetCode: undefined,
        sheetName: undefined,
        sheetLevel: undefined,
        sheetUnit: undefined,
        sheetTaxRate: undefined,
        lowerFlag: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sheetName: [{ required: true, message: "清单名称不能为空", trigger: "blur" }],
        //sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        // sheetLevel: [{ required: true, message: "清单级别不能为空", trigger: "blur" }],
        // sheetUnit: [{ required: true, message: "清单单位不能为空", trigger: "blur" }],
        // sheetTaxRate: [{ required: true, message: "税率不能为空", trigger: "blur" }],
        // remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询标准投标清单列表 */
    getList () {
      this.loading = true;
      listStandardSheet(this.queryParams).then((response) => {
        this.standardSheetList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        sheetId: undefined,
        parentId: undefined,
        sort: undefined,
        sheetCode: undefined,
        sheetName: undefined,
        sheetLevel: undefined,
        sheetUnit: undefined,
        sheetTaxRate: undefined,
        remark: undefined,
        delFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        lowerFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.sheetId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd (type) {
      this.reset();
      if (type == 1) {
        this.form.parentId = 0;
        this.form.sheetLevel = 1;
      } else {
        if (type?.sheetLevel == 1) {
          this.form.parentId = type.id;
          this.form.sheetLevel = 2;
        } else if (type?.sheetLevel == 2) {
          this.form.sheetLevel = 3;
          this.form.parentId = type.id;
        }
      }
      this.open = true;
      this.title = "添加标准投标清单";
    },
    changeMessage () {
      this.$forceUpdate();
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = row;
      this.form.sheetId = row.id;
      this.form.sheetName = row.label;
      this.form.sort = row.weight;
      this.open = true;
      this.title = "修改标准投标清单";
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.sheetId != null) {
            updateStandardSheet(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addStandardSheet(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const sheetIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除标准投标清单名称为"' + row.label + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delStandardSheet(sheetIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "base/standardSheet/export",
        {
          ...this.queryParams
        },
        `standardSheet_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
